package org.example.schoolbus.controller;
import org.example.schoolbus.entity.ResultBody;
import org.example.schoolbus.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
@RestController
@RequestMapping("/Register")
@CrossOrigin(origins ="*")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    @RequestMapping("/RegisterUser")
    public ResultBody RegisterUser(@RequestBody Map<String,Object> map) {
        //首先判断参数是否为空
        if (map.get("userstyle") == null || map.get("username") == null || map.get("useremail") == null || map.get("userphone") == null || map.get("userpassword") == null||map.get("repassword")==null) {
            return new ResultBody(404, "缺少信息", false);
        }
        String userstyle = map.get("userstyle").toString();//0为学生，1为管理员
        String username = map.get("username").toString();
        String useremail = map.get("useremail").toString();
        String userphone = map.get("userphone").toString();
        String userpassword = map.get("userpassword").toString();
        String repassword = map.get("repassword").toString();//密码确认
        //先进行去重判断，然后进行密码确认，再将数据存入数据库中
        int list = registerService.checkUseUser(username, useremail, userphone);
        if (list==0) {
            switch (userstyle) {
                case "0":
                    if(repassword.equals(userpassword)) {
                        if (registerService.RegisterUser(userstyle, username, useremail, userphone, userpassword) > 0 ) {
                            return new ResultBody(200, "学生注册成功", true);
                        } else {
                            return new ResultBody(400, "学生注册失败", false);
                        }
                    }
                    else
                        return new ResultBody(400,"密码确认失败",false);
                case "1":
                    if(repassword.equals(userpassword)) {
                        if (registerService.RegisterUser(userstyle, username, useremail, userphone, userpassword) > 0) {
                            return new ResultBody(200, "管理员注册成功", true);
                        } else {
                            return new ResultBody(400, "管理员注册失败", false);
                        }
                    }
                    else
                        return new ResultBody(400,"密码确认失败",false);
                default:
                    return new ResultBody(400, "未知用户类型", false);
            }
        }
        else if(list==1) {
            return new ResultBody(400,"用户名被注册过",false);
        }
        else if (list==2) {
            return new ResultBody(400,"用户邮箱被注册过",false);
        }
        else {
            return new ResultBody(400,"用户电话号码被注册过",false);
        }

    }

}

